package com.huazi.eduservice.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huazi.commonutils.CommentResult;
import com.huazi.eduservice.client.CommentClient;
import com.huazi.eduservice.entity.EduCourse;
import com.huazi.eduservice.mapper.EduCourseMapper;
import com.huazi.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huazi.eduservice.vo.CourseComment;
import com.huazi.eduservice.vo.CourseWebVo;
import com.huazi.eduservice.vo.FinalInfo;
import com.huazi.eduservice.vo.FrontCourseInfo;
import com.huazi.servicebase.exceptionhandler.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.xml.bind.annotation.W3CDomHandler;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huazi
 * @since 2021-03-18
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    CommentClient client;
    @Override
    public int addCourseInfo(EduCourse course) {
        int insert = baseMapper.insert(course);
        if(insert<=0) {
            throw new MyException("课程添加失败！");
        }
        return insert;
    }

    @Override
    public FinalInfo getFinalInfo(String id) {
        return baseMapper.getFinalInfo(id);
    }

    @Override
    public void updateStatus(String id) {
        baseMapper.updateStatus(id);
    }
    //首页数据显示
    @Cacheable(key="'getIndexCourses'",value = "course")
    @Override
    public CommentResult getIndexCourses(Integer current, Integer size) {
        Page<EduCourse> page=new Page<>(current,size);//原始课程分页对象
        //获取数据库信息
        baseMapper.selectPage(page,null);
        //获取数据库查询出来的课程列表
        List<EduCourse> courses=page.getRecords();
        //调用对象获取新的课程集合
        List<CourseComment> ccList = getCourseCommentList(courses);
        //新课程分页对象
        Page<CourseComment> ccPage=new Page<>(current,size);
        //新分页对象赋值
        ccPage.setRecords(ccList).setTotal(page.getTotal()).setPages(page.getPages());
        //返回新分页对象
        return CommentResult.getOK().data("getIndexCourses",ccPage);
    }

    //获取指定页条数的信息
    @Override
    public CommentResult getCourseLists(Integer current, Integer size) {
        Page<EduCourse> page=new Page<>(current,size);
        baseMapper.selectPage(page, null);
        return CommentResult.getOK().data("getCourseLists",page);
    }
    //条件分页组合查询
    @Override
    public CommentResult getCourseFrontList(Integer current, Integer size, FrontCourseInfo courseInfo) {
        Page<EduCourse> page=new Page<>(current,size);
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        //判断是否刷选1级类别
        if(!StringUtils.isEmpty(courseInfo.getSubjectParentId()))
            wrapper.eq("subject_parent_id",courseInfo.getSubjectParentId());
        //判断是否刷选1级类别
        if(!StringUtils.isEmpty(courseInfo.getSubjectId()))
            wrapper.eq("subject_id",courseInfo.getSubjectId());
        //判断是否进行销售排序
        if(!StringUtils.isEmpty(courseInfo.getBuyCountSort()))
            wrapper.orderByDesc("buy_count");
        //判断是否进行时间排序
        if(!StringUtils.isEmpty(courseInfo.getGmtCreateSort()))
            wrapper.orderByDesc("gmt_create");
        //判断是否进行价格排序
        if(!StringUtils.isEmpty(courseInfo.getPriceSort()))
            wrapper.orderByDesc("price");
        baseMapper.selectPage(page,wrapper);
        List<EduCourse> records = page.getRecords();
        List<CourseComment> ccList = getCourseCommentList(records);
        Page<CourseComment> ccPage=new Page<>(current,size);
        ccPage.setRecords(ccList).setTotal(page.getTotal()).setPages(page.getPages());
        return CommentResult.getOK().data("frontInfos",ccPage);
    }

    //课程详情信息获取
    @Override
    public CourseWebVo getBaseCourseInfo(String id) {
        CourseWebVo baseCourseInfo = baseMapper.getBaseCourseInfo(id);
        return baseCourseInfo;
    }
    //根据一个课程集合，遍历课程获取课程对应评论数，返回一个带评论数的课程对象集合
    public List<CourseComment> getCourseCommentList(List<EduCourse> courses){
        //带评论数课程集合
        List<CourseComment> ccs=new ArrayList<>();
        //将数据库的课程信息遍历，根据id远程调用获取评论数
        for (EduCourse course: courses
        ) {
            //创建前端课程列表数据显示对象
            CourseComment cc = new CourseComment();
            //根据课程id远程调用获取评论数
            Integer count = client.getCommentCount(course.getId());
            cc.setCover(course.getCover())
                    .setId(course.getId())
                    .setCommentCount(count)
                    .setPrice(course.getPrice())
                    .setTitle(course.getTitle())
                    .setViewCount(course.getViewCount());
            ccs.add(cc);
        }
        return ccs;
    }
}
